<template>
	<view class="page-backgroud">
		<!-- 轮播图 -->
		<!--circular用来设置循环轮播   indicator-dots设置下面小圆点用来点击 -->
				<!--circular用来设置循环轮播   indicator-dots设置下面小圆点用来点击 -->
		<view class="uni-margin-wrap">
 			<swiper class="swiper" circular indicator-dots autoplay="true">
 				<!-- v-for循环遍历数组 -->
 				<swiper-item v-for="(item,index) in swipers" :key="index">
 					<image :src="item"></image>
 				</swiper-item>
 			</swiper>
		</view>
		
		<view class="row-50-width">
			<!-- 店铺 -->
			<view class="left-right-half-box" v-for="(item, index) in massageList" 
				:key="item.id"  @click="toDoctorPage(item.id, item.name)">
				<view class="left-right-inbox">
					<image style="width: 100%;height: 170px;"  :src="item.picture"></image>
					<view class="row-width">
						<text class="massage-text">{{item.name}}</text>
					</view>
					<view class="row-width">
						<text class="blue-title">地址：</text>
						<text class="address-phone-text cut-long-text">{{item.address}}</text>
					</view>
					<view class="row-width">
						<text class="blue-title">电话：</text>
						<text class="address-phone-text cut-long-text">{{item.phone}}</text>
					</view>
				</view>
			</view>
			<!-- 预约商品 -->
			<view class="left-right-half-box">
				<view class="left-right-inbox" @click="toProduct()">
					<image style="width: 100%;height: 170px;" :src="productImgUrl" ></image>
					<view style="text-align: center;align-items:center;width:100%;height: 150rpx;">
						<text class="product-text">{{name}}</text>
					</view>
				</view>
			</view>
			
		</view>
	</view>
</template>

<script>
	import {getAllMassage} from '@/common/api/massage.js'
	import {error} from '@/common/js/errorTips.js'
	import {getWallPaper} from '@/common/api/wallPaper.js'
	import {getConfig} from '@/common/api/config.js'
	import { wxLogin } from '@/common/api/quickRegister.js'
	import { setToken, getToken } from '@/common/utils/auth.js';
	var _self = 1,timer = null;
	export default {
		data() {
			return {
				name:'',
				productImgUrl:'',
				// 轮播图
				swipers:[],
				massageList: []
			}
		},
		methods: {
			//获取配置
			getConfig(){
				let that = this
				getConfig().then(res =>{
					if(res.data.code === 200){
						// 1显示 0不显示
						that.productImgUrl = res.data.data.url
						that.name = res.data.data.name
					}
				})
			},
			// 下拉刷新
			onPullDownRefresh() {
				let that = this
				setTimeout(function() {
						that.getConfig()
						that.getWallPaper()
						that.getAllMassageList()
						uni.showToast({
							title: '刷新成功',
							icon: 'success'
						})
						uni.stopPullDownRefresh()
				}, 1000);
			},
			// 获取首页轮播图图片地址
			getWallPaper: function(){
				this.swipers = []
				//console.log('获取wallpaper')
				getWallPaper().then(res =>{
					let data = res.data
					let that = this
					if(data.code === 200){
						data.data.forEach(function(item,index){
							that.swipers.push(item.img)
						})
					}
				})
			},
			// 跳转预约商品页面
			toProduct: function(){
				uni.navigateTo({
					url:'/pages/home/appointmentHome/product/product'
				})
			},
			// 获取今天的日期
			getCurrentDate: function() {
				let currentDate = new Date()
				this.dateList = formDate(currentDate, 'week-date');
			},
			// 跳转到获取专科页面
			toDepartmentPage: function(id, name) {
				let massage = {
					id: id,
					name: name
				}
				uni.setStorageSync('massage', JSON.stringify(massage))
				uni.navigateTo({
					 url: '/pages/home/appointmentHome/' + 
					 'selectDepartment/selectDepartment?massageID=' + id
				})
			},
			//添加
			//跳转到选择医生页面
			toDoctorPage:function(id,name){
				//获取今天日期
				let currentDate = new Date()
				let massage = {
					id: id,
					name: name,
				}
				uni.setStorageSync('massage',JSON.stringify(massage))
				uni.navigateTo({
					url: '/pagesB/pages/appointPages/timeDoctor/timeDoctor?massageId=' + id
					})
			},
			// 获取店铺的信息
			getAllMassageList: function() {
				getAllMassage(1, 50, '').then(res => {
					if (res.data.code === 200) {
						this.massageList = res.data.data.list
						uni.setStorageSync('firstMassageId', this.massageList[0].id)
					}
				}).catch(() => {
					error('网络')
				})
			},
			//每次打开页面重新获取token
			getToken(){
				//如果登录了
				if(uni.getStorageSync('isAlreadyLogin')== true){
					let data = {
						authType:'wx',
						cellphone:uni.getStorageSync('phoneNum'),
						childName:uni.getStorageSync('childName'),
						openId:uni.getStorageSync('openid'),
						password:'',
						url:uni.getStorageSync('avatarUrl'),
						username:uni.getStorageSync('username')
					}
					wxLogin(data).then(res =>{
						if(res.data.code === 200){
							setToken(res.data.data)	
						}
						
					})
				}
			}
		},
		created() {
			this.getConfig()
			this.getAllMassageList()
			this.getWallPaper()
			this.getToken()
		}
	}
</script>

<style lang="scss">
	@import '@/common/scss/common.scss';
	.uni-margin-wrap{
		swiper{
			width: 690rpx;
			width: 100%;
			image {
				width: 100%;
				height:100%;
			}
		}
	}
	.swiper{
		height:400rpx;
	}
	// 先将页面分成左右排列
	.row-50-width {
		width: 100%;
		//@include flex-direction(row);
	.product-text{
						@include font-style(20px, bold, #000000);
					}
		// 将页面分成左右两半
		.left-right-half-box {
			width: 50%;
			height: 100%;
			//@include flex-direction(column);
			float: left;
			// 每个小块的样式
			.left-right-inbox {
				@include width-margin(91%, 100%);
				background: #FFFFFF;
				//@include flex-direction(column);
				border: 1px solid #BBBBBB;
				border-radius: 8px;
				padding: 10rpx;
				margin-top: 20rpx;
				.row-width {
					width: 100%;
					height: 50rpx;
					//@include flex-direction(row);
					align-items: center;

					.icon {
						@include icon-size(50rpx, 50rpx);
						margin-right: 10rpx;
					}

					.massage-text {
						@include font-style(15px, bold, #000000);
					}
					

					.blue-title {
						width: 30%;
						@include font-style(16px, 400, $major-color);
					}

					.address-phone-text {
						width: 68%;
						@include font-style(16px, 400, $gray-color);
					}
				}
			}
		}
	}
</style>
